﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using ClassLibrary1.Common;

namespace ClassLibrary1.UI
{
    public partial class FilmShowImageControl : UserControl
    {
        public event EventHandler<string> OnClickPicture;//缩略图点击事件，返回当前图片路径

        private float _pictureWidth;
        private float _pictureHeight;
        private float _pictureLength;
        private float _pictureGap;
        private float _sqWidth;
        private float _sqHeight;
        private float _sqLength;

        private List<PictureBox> _pictureBoxs = new List<PictureBox>();
        private int _selectIndex = -1;

        private float _firstPictureX;
        private float _firstPictureY;

        private Graphics _gSq;
        private Graphics _gFlag;

        private List<FileInfo> fileInfos = new List<FileInfo>();
        private int _aheadIndex;

        private string _imageWatcherPath;

        [Category("自定义"), Description("监视图片文件路径")]
        public string ImageWatcherPath
        {
            get { return _imageWatcherPath; }
            set
            {
                _imageWatcherPath = value;
                fileSystemWatcher1.Path = value;
                _selectIndex = -1;
                _aheadIndex = 0;
                fileInfos.Clear();
                try
                {
                    fileInfos = new DirectoryInfo(value).GetFiles("*.jpg").
                            Union(new DirectoryInfo(value).GetFiles("*.bmp")).ToList();
                }
                catch (Exception ex)
                {

                }
                
                Invalidate();
            }
        }



        public FilmShowImageControl()
        {
            InitializeComponent();
            

            fileSystemWatcher1.Changed += FileSystemWatcher1_Changed;
            for (int i = 0; i < 10; i++)
            {
                _pictureBoxs.Add(new PictureBox()
                {
                    SizeMode = PictureBoxSizeMode.Zoom,
                    Tag = i,
                    BackColor = SystemColors.ControlDarkDark
                });
            }

            _pictureBoxs.ForEach((p) =>
            {
                p.Click += P_Click;
                Controls.Add(p);
            });

            FilmShowImageControl_Resize(null, null);
        }

        private void FileSystemWatcher1_Changed(object sender, FileSystemEventArgs e)
        {
            throw new NotImplementedException();
        }

        public void Test()
        {
            _gSq?.Clear(SystemColors.ControlDark);
        }

        private void P_Click(object sender, EventArgs e)
        {
            if (sender is PictureBox p)
            {
                _selectIndex = (int)p.Tag;
                if (p.Image!=null)
                {
                    OnClickPicture?.Invoke(this, fileInfos[_aheadIndex + _selectIndex].FullName);
                }               
                Invalidate();
                //MessageBox.Show(p.Tag.ToString());
            }
        }

        private void FilmShowImageControl_Paint(object sender, PaintEventArgs e)
        {
            _gSq = CreateGraphics();
            _gFlag = CreateGraphics();

            _pictureLength = Width / 10;
            _pictureGap = _pictureLength / 15;
            _pictureWidth = _pictureLength - _pictureGap;
            _pictureHeight = (float)(Height * 0.7);
            _firstPictureX = _pictureGap;
            _firstPictureY = (float)(Height * 0.15);

            _sqLength = _pictureWidth / 5;
            _sqWidth = _sqLength / 2;
            _sqHeight = _firstPictureY / 3;

            //Graphics g = CreateGraphics();
            for (int i = 0; i < 10; i++)
            {
                _pictureBoxs[i].Height = (int)_pictureHeight;
                _pictureBoxs[i].Width = (int)_pictureWidth;
                _pictureBoxs[i].Location = new Point((int)(_firstPictureX + i * _pictureLength), (int)_firstPictureY);
                for (int j = 0; j < 5; j++)
                {
                    RectangleF sqRectF = new RectangleF(
                        _pictureBoxs[i].Location.X + j * _sqLength,
                        _pictureBoxs[i].Location.Y - _sqHeight * 2,
                        _sqWidth,
                        _sqHeight
                        );
                    _gSq.FillRectangle(new SolidBrush(Color.White), sqRectF);
                }
                for (int k = 0; k < 5; k++)
                {
                    RectangleF sqRectF = new RectangleF(
                        _pictureBoxs[i].Location.X + k * _sqLength,
                        _pictureBoxs[i].Location.Y + _pictureHeight + _sqHeight,
                        _sqWidth,
                        _sqHeight
                        );
                    _gSq.FillRectangle(new SolidBrush(Color.White), sqRectF);
                }

                //加载图像
                if (_aheadIndex + i < fileInfos.Count)
                {
                    var imagePath = fileInfos[_aheadIndex + i].FullName;
                    if (File.Exists(imagePath))
                    {
                        Image reduceImage = new ImageClass(imagePath).GetReducedImage((int)_pictureWidth, (int)_pictureHeight);
                        _pictureBoxs[i].Image = reduceImage;
                    }

                }

                //标记选中
                if (_selectIndex == i&& _pictureBoxs[i].Image!=null)
                {
                    _gFlag.DrawLines(new Pen(Color.Yellow, _pictureGap),
                        new PointF[]
                        {
                           new PointF(_pictureBoxs[i].Location.X,_pictureBoxs[i].Location.Y),
                           new PointF(_pictureBoxs[i].Location.X,_pictureBoxs[i].Location.Y+_pictureHeight),
                           new PointF(_pictureBoxs[i].Location.X+_pictureWidth,_pictureBoxs[i].Location.Y+_pictureHeight),
                           new PointF(_pictureBoxs[i].Location.X+_pictureWidth,_pictureBoxs[i].Location.Y),
                           new PointF(_pictureBoxs[i].Location.X,_pictureBoxs[i].Location.Y)
                        }
                        );
                }
            }
        }

        private void FilmShowImageControl_Resize(object sender, EventArgs e)
        {
            _gSq = CreateGraphics();
            _gFlag = CreateGraphics();
            _pictureLength = Width / 10;
            _pictureGap = _pictureLength / 15;
            _pictureWidth = _pictureLength - _pictureGap;
            _pictureHeight = (float)(Height * 0.7);
            _firstPictureX = _pictureGap;
            _firstPictureY = (float)(Height * 0.15);

            _sqLength = _pictureWidth / 5;
            _sqWidth = _sqLength / 2;
            _sqHeight = _firstPictureY / 3;

            for (int i = 0; i < 10; i++)
            {
                _pictureBoxs[i].Height = (int)_pictureHeight;
                _pictureBoxs[i].Width = (int)_pictureWidth;
                _pictureBoxs[i].Location = new Point((int)(_firstPictureX + i * _pictureLength), (int)_firstPictureY);
                for (int j = 0; j < 5; j++)
                {
                    RectangleF sqRectF = new RectangleF(
                        _pictureBoxs[i].Location.X + j * _sqLength,
                        _pictureBoxs[i].Location.Y - _sqHeight * 2,
                        _sqWidth,
                        _sqHeight
                        );
                    _gSq.FillRectangle(new SolidBrush(Color.White), sqRectF);
                }
                for (int k = 0; k < 5; k++)
                {
                    RectangleF sqRectF = new RectangleF(
                        _pictureBoxs[i].Location.X + k * _sqLength,
                        _pictureBoxs[i].Location.Y + _pictureHeight + _sqHeight,
                        _sqWidth,
                        _sqHeight
                        );
                    _gSq.FillRectangle(new SolidBrush(Color.White), sqRectF);
                }

                //加载图像
                if (_aheadIndex + i < fileInfos.Count)
                {
                    var imagePath = fileInfos[_aheadIndex + i].FullName;
                    if (File.Exists(imagePath))
                    {
                        Image reduceImage = new ImageClass(imagePath).GetReducedImage((int)_pictureWidth, (int)_pictureHeight);
                        _pictureBoxs[i].Image = reduceImage;
                    }

                }

                //标记选中
                if (_selectIndex == i && _pictureBoxs[i].Image != null)
                {
                    _gFlag.DrawLines(new Pen(Color.Yellow, _pictureGap),
                        new PointF[]
                        {
                           new PointF(_pictureBoxs[i].Location.X,_pictureBoxs[i].Location.Y),
                           new PointF(_pictureBoxs[i].Location.X,_pictureBoxs[i].Location.Y+_pictureHeight),
                           new PointF(_pictureBoxs[i].Location.X+_pictureWidth,_pictureBoxs[i].Location.Y+_pictureHeight),
                           new PointF(_pictureBoxs[i].Location.X+_pictureWidth,_pictureBoxs[i].Location.Y),
                           new PointF(_pictureBoxs[i].Location.X,_pictureBoxs[i].Location.Y)
                        }
                        );
                }
            }
        }
    }
}
